/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 */

import { isWindows } from '@ohos/hvigor';
import path from 'path';

import { FileUtil } from '../utils/file-util.js';

import { BaseCommandBuilder } from './base-command-builder.js';

export class NpmCommandBuilder extends BaseCommandBuilder {
  constructor(npmPath?: string) {
    super([]);
    const npmTool = isWindows() ? 'npm.cmd' : 'npm';
    const npm = npmPath ? path.join(npmPath, npmTool) : npmTool;
    this.commandList.push(npm);
  }

  runScript(script: string): NpmCommandBuilder {
    FileUtil.checkPathLength(script);
    this.commandList.push('run');
    this.commandList.push(script);
    return this;
  }

  addScriptsOptions(): NpmCommandBuilder {
    this.commandList.push('--scripts-prepend-node-path');
    this.commandList.push('true');
    return this;
  }

  install(): NpmCommandBuilder {
    this.commandList.push('install');
    return this;
  }
}
